<?php
class Category_home_model extends CI_Model{
   
   
    public function list_cate()
    {
        $sql = "SELECT *
                FROM `tbl_category`
                ORDER BY `order`";
        $query = $this->db->query($sql);
        return $query->result_array();
    }
    
    public function get_info($col, $get_info)
    {
       
        $sql = "SELECT *
                FROM tbl_category
                WHERE $col = $get_info";
        $query = $this->db->query($sql);
        return $query->result_array();
    }
    public function is_parent($cate_id = '')
    {
        $sql = "SELECT * "
                . "FROM tbl_category "
                . "WHERE cate_parent={$cate_id}";
        $query = $this->db->query($sql);
        if($query->num_rows() > 0)
        {
            return TRUE;
        }
        else
        {
            return FALSE;
        }
    }
    public function list_child($parent_id)
    {
        $listAll = $this->list_cate();
        $list_child = '';
        if($listAll != null)
        {
            foreach($listAll as $key => $val)
            {
                
                 if($val['cate_parent'] == $parent_id)
                 {
                    $list_child .= $val['cate_id'].",";
                    if($this->is_parent($val['cate_id']))
                    {
                       $list_child .= $this->list_child($val['cate_id']);
                    }    
                }
            }
       }
       return $list_child;    
    }
    public function show_menu($listAll, $parent_id)
   {
       $baseURL = base_url();
       $html = '';
       if($listAll != null)
       {
            foreach($listAll as $key => $val)
            {
                 if($val['cate_parent'] == $parent_id)
                 {
                    $list_child = $this->list_child($val['cate_id']);
                    
                    $list_sub = $list_child.$val['cate_id'];
                    
                    $sql = "SELECT COUNT(DISTINCT `pro_id`) AS total
                            FROM tbl_cate_pro
                            WHERE cate_id
                            IN ($list_sub)";
                    $query = $this->db->query($sql);
                    $count = mysql_fetch_assoc(mysql_query($sql));
                    $html .= "<li><a href = '{$baseURL}{$val['cate_id']}/category.html#cate'>".$val['cate_name']."({$count['total']})</a>";
                    if($list_child != '')
                    {
                        $html .='<ul>';
                        $html .= $this->show_menu($listAll, $val['cate_id']);
                        $html .='</ul>';
                    }
                    $html .='</li>';
                    
                 }
            }
       }
       return $html; 
   }
}
